package org.example.cloud.controller;


import org.example.cloud.bean.User;
import org.example.cloud.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;


/**
 * @Author: Xiao Lan
 * @Date: 2025/6/4 20:12
 * @Description:
 */
@RestController

public class UserProfileController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/api/profile")
    @CrossOrigin(origins = "http://localhost:8080")
    public User getUserProfile(@RequestHeader("X-Username") String encodedUsername) {





        try {
            // 解码用户名
            String username = URLDecoder.decode(encodedUsername, StandardCharsets.UTF_8.toString());

            User user = userMapper.findByUsername(username);

            System.out.println("User: " + user);
            System.out.println(username);

            if (user == null) {
                throw new RuntimeException("User not found");
            }

            user.setPassword(null);
            return user;

        } catch (Exception e) {
            throw new RuntimeException("用户名解码失败", e);
        }
    }
    }